﻿/***********
 * 王海龙
 * 站点基础配置属性加载
 * **********************/
//全局属性配置
Vue.mixin({
    User: {
        UserName: sessionStorage.getItem("UserName") ? sessionStorage.getItem("UserName") : "",
        Password: '',
        RememberMe: false,
        passwordType: 'password',
        MyPermission: sessionStorage.getItem("MyPermission") ? sessionStorage.getItem("MyPermission") : "",
        UserId: sessionStorage.getItem("UserId") ? sessionStorage.getItem("UserId") : "",
        Token: sessionStorage.getItem("Token") ? sessionStorage.getItem("Token") : ""

    },

    methods: {
        Login() {//登录
            const self = this;
            this.$validator.errors.clear();
            this.$validator.validateAll().then((result) => {//js验证
                if (result) {
                    self.post({
                        url: '/account/login',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        data: { UserName: self.UserName, Password: self.Password },
                        success: function (result) {
                            self.$options.User = result;
                            sessionStorage.setItem("UserId", result.UserId);
                            sessionStorage.setItem("UserName", result.UserName);
                            sessionStorage.setItem("Token", result.Token);
                            if (result.MyPermission) {
                                sessionStorage.setItem("MyPermission", JSON.stringify(result.MyPermission));
                            }

                            if (result.Message) {
                                window.location.href = result.Message;
                            }
                            else {
                                window.location.href = "/";
                            }
                            self.$validator.errors.add({ msg: "登录成功" });

                        },
                        error: function (message) {
                            self.$validator.errors.add({ field: "errormsg", msg: message });
                        }
                    });
                }

            });

        },
        post(options) {//全局请求设置
            this.$http.post(options.url, options.data, { emulateJSON: true }).then(function (resp) {
                console.log(resp);
                if (!resp.body.IsError) {
                    if (options.success) {
                        options.success(resp.body.Data, resp.body.Message);
                    }
                    //  self.$validator.errors.add({ field:"ajaxError", msg: "登录成功" });
                }
                else {
                    if (options.error) {
                        options.error(resp.body.Message);
                    }
                }
            },
                function (resp) {
                    if (options.error) {
                        options.error('请求失败：' + resp.status + ',' + resp.statusText);
                    } else {
                        console.log('请求失败：' + resp.status + ',' + resp.statusText);
                    }
                });

        },
        get(options) {
            this.$http.get(options.url, options.data).then(function (resp) {
                console.log(resp);
                if (options.success) {
                    options.success(resp.body);
                }
            },
                function (resp) {
                    if (options.error) {
                        options.error('请求失败：' + resp.body.status + ',' + resp.statusText);
                    } else {
                        console.log('请求失败：' + resp.status + ',' + resp.statusText);
                    }
                });
        }
    }
});
//系统菜单组件
Vue.component('v-menu-item', {
    // 声明 props
    props: ['page','children'],
    // 同样也可以在 vm 实例中像 "this.message" 这样使用
    template: '<li class="nav-left-menu-link">'
        + '<a href="javascript://">'
        + '<i class= "fa" :class= "page.Icon? page.Icon: \' fa-file-o\'" ></i>'
        + '<span class="nav-label">{{ page.LocalName }}</span>'
        +'<span class="fa arrow"></span>'
        + '</a><ul class="nav  nav nav-third-level" v-if="children">'
        + '<li class="nav-left-menu-link" v-for="child in children">'
        + '<a :href="child.RouteUrl?child.RouteUrl:\'\'">'
        + '<i class="fa " :class="child.Icon? child.Icon : \' fa-file-o\'"></i>'
        + '<span class="nav-label">{{ child.LocalName }}</span>'
        + '</a></li></ul></li>',
        methods: {
            GetChildMenu: function (parentid) {//获取子菜单目录
                var pages = this.results.MyPermission;
                console.log(page);
                var childMenu = [];
                pages.forEach(item => {
                    if (parentid == "" && (!item.ParentId || item.ParentId == "")) {
                        childMenu.push(item);
                    }
                    else {
                        if (item.ParentId == parentid) {
                            childMenu.push(item);
                        }
                    }
                });
                return childMenu;
            }
        }
})
Vue.component('v-html-panel', () => import('/home/index-main.html'))